#!/usr/bin/python
import sqlite3
import time
from datetime import datetime
import random

class DataBase:
    def test(self):
        cx = sqlite3.connect('./temp.db')
        cu = cx.cursor()
        print("数据库连接成功!")
        # # cu.execute(
        # #     "create table tempdata (id integer primary key AUTOINCREMENT,loc integer,temp integer,data integer)")
        # for i in range(1100):
        #     cu.execute("insert into tempdata (loc,temp,data) VALUES(?,?,?)", (1, 2, time.time()))
        # data = cu.execute("select * from tempdata")
        # for d in data:
        #     print(d[0])
        #     print(d[1])
        #     print(d[2])
        #     print(d[3])

        # cu.execute(
        #     "create table tempdata2 (id integer primary key AUTOINCREMENT,loc integer,temp1 integer,temp2 integer, temp3 integer ,date text)")
        # for i in range(16):
        #     cu.execute("insert into tempdata2 (loc,temp1, temp2, temp3, date) VALUES(?,?,?,?,?)", (i, random.randint(32,35), random.randint(38,43), random.randint(36,40), datetime.now().strftime('%Y/%m/%d %H-%M-%S')))
            # cu.execute("insert into tempdata2 (loc,temp1, temp2, temp3, date) VALUES(?,?,?,?,?)", (i, random.randint(32,35), random.randint(38,43), random.randint(36,40), '2021/11/21 12-02-22'))
        # data = cu.execute("select * from tempdata2 where date like '2021/11/21%'")
        data = cu.execute("select * from tempdata2 where date like '2021/11/2%' and (temp2 > 40 and temp3 > 38)")
        # cu.execute("delete from tempdata1")
        temp1Arr = []
        for d in data:
            temp1Arr.append(d[2])
            print(d[0],d[1],d[2],d[3],d[4],d[5])
            # print(d[1])
            # print(d[2])
            # print(d[3])
            # print(d[4])
        cx.commit()
        cx.close()
        print(temp1Arr)
if __name__ == '__main__':
    db = DataBase()
    db.test()
    print(time.time())
